﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;
using System.Collections.Generic;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.Web.UI.WebControls;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using WCSFContrib.ECommerceCatalog.BusinessEntities;
using WCSFContrib.ECommerceCatalog.BusinessEntities.Enums;
using WCSFContrib.ECommerceCatalog.User.Views;

namespace WCSFContrib.ECommerceCatalog.User.Views
{
    public partial class Default : Microsoft.Practices.CompositeWeb.Web.UI.Page, IDefaultView
    {
        private DefaultPresenter _presenter;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this._presenter.OnViewInitialized();
            }
            this._presenter.OnViewLoaded();
        }

        [CreateNew]
        public DefaultPresenter Presenter
        {
            set
            {
                this._presenter = value;
                this._presenter.View = this;

                this._presenter.UserName = User.Identity.Name;
            }
        }

        protected void GoToCartLinkButton_Click(object sender, EventArgs e)
        {
            _presenter.OnGoToCart();
        }

        protected void AddressesContainerDataSource_Deleted(object sender, ObjectContainerDataSourceStatusEventArgs e)
        {
            string userName = User.Identity.Name;
            _presenter.OnDeleteAddress((Address)e.Instance);
        }

        protected void AddressesContainerDataSource_Updated(object sender, ObjectContainerDataSourceStatusEventArgs e)
        {
            string userName = User.Identity.Name;
            _presenter.OnEditAddress((Address)e.Instance);
        }

        protected void PaymentsObjectContainerDataSource_Deleted(object sender, ObjectContainerDataSourceStatusEventArgs e)
        {
            string userName = User.Identity.Name;
            _presenter.OnDeletePaymentMethod((Payment)e.Instance);
        }

        protected void PaymentsObjectContainerDataSource_Updated(object sender, ObjectContainerDataSourceStatusEventArgs e)
        {
            string userName = User.Identity.Name;
            _presenter.OnEditPaymentMethod((Payment)e.Instance);
        }

        protected void AddAddressButton_Click(object sender, EventArgs e)
        {
            string userName = User.Identity.Name;
            Address address = new Address(-1, StreetTextBox.Text, CityTextBox.Text, StateTextBox.Text,
                                              CountryTextBox.Text, PhoneTextBox.Text);

            _presenter.OnAddAddress(address);

            StreetTextBox.Text = string.Empty;
            CityTextBox.Text = string.Empty;
            StateTextBox.Text = string.Empty;
            CountryTextBox.Text = string.Empty;
            PhoneTextBox.Text = string.Empty;
        }

        protected void AddPaymentMethodButton_Click(object sender, EventArgs e)
        {
            string userName = User.Identity.Name;
            PaymentType paymentType = (PaymentType)int.Parse(PaymentTypeDropDownList.SelectedItem.Value, CultureInfo.CurrentCulture);
            Payment payment = new Payment(-1, paymentType, InfoTextBox.Text);

            _presenter.OnAddPaymentMethod(payment);

            PaymentTypeDropDownList.SelectedIndex = 0;
            InfoTextBox.Text = string.Empty;
        }

        #region IDefaultView Members

        public void ShowAddressList(ICollection<Address> addressList)
        {
            AddressesContainerDataSource.DataSource = addressList;
        }

        public void ShowPaymentsMethods(ICollection<Payment> paymentList)
        {
            PaymentsObjectContainerDataSource.DataSource = paymentList;
        }

        public void ShowOrders(ICollection<Order> orders)
        {
            OrdersObjectContainerDataSource.DataSource = orders;
        }

        public int TotalAddressCount
        {
            set
            {
                AddressesContainerDataSource.TotalRowCount = value;

                ViewState["TotalAddressCount"] = value;
            }
            get
            {
                return (int)(ViewState["TotalAddressCount"] ?? 0);
            }
        }

        public int TotalPaymentsCount
        {
            set
            {
                PaymentsObjectContainerDataSource.TotalRowCount = value;

                ViewState["TotalPaymentsCount"] = value;
            }
            get
            {
                return (int)(ViewState["TotalPaymentsCount"] ?? 0);
            }
        }

        public int TotalOrdersCount
        {
            set
            {
                OrdersObjectContainerDataSource.TotalRowCount = value;

                ViewState["TotalOrdersCount"] = value;
            }
            get
            {
                return (int)(ViewState["TotalOrdersCount"] ?? 0);
            }
        }

        #endregion
    }
}

